package org.example.work.work;

import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.example.work.Mapper.Busermapper;
import org.example.work.Mapper.Ordersmapper;
import org.example.work.Mapper.Ousermapper;
import org.example.work.Mapper.Usermapper;
import org.example.work.entity.buser;
import org.example.work.entity.orders;
import org.example.work.entity.ouser;
import org.example.work.entity.user;
import org.example.work.service.insertuser;
import org.example.work.service.updata;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;


import java.util.List;
import java.util.Map;

@RestController

public class chuli {
    @Autowired
    private Usermapper userm;
    @Autowired
    private Ousermapper ouserm;
    @Autowired
    private Busermapper buserm;
    @Autowired
    private Ordersmapper orderm;
    @RequestMapping("/chu")
    public user chu() {

        return userm.selectByID(54);
    }
    @RequestMapping("/chu2")
    public List<ouser> chu2() {

        return ouserm.selectAll();
    }
//    前端使用 Axios 发送请求时，默认的 Content-Type 是 application/json，而后端代码使用 request.getParameter 来获取参数，这种方式适用于 application/x-www-form-urlencoded 格式的数据，而不是 JSON 数据。
//       JSON数据的获取
//         String name = requestParams.get("name");
//        String password = requestParams.get("password");
//    在后端代码中，@RequestBody Map<String, String> requestParams，使用 @RequestBody 来接收 JSON 数据，而不是 request.getParameter。
@Autowired
private insertuser insert;
    @CrossOrigin(origins = "*", methods = { RequestMethod.GET, RequestMethod.POST })
//    设置局部跨域请求，使axios请求能访问，其实效果一样
    @RequestMapping("/chu3")
    public int chu3(HttpServletRequest request) {
//        HttpServletRequest适用于get请求
        // 设置允许跨域的头部信息
//        response.setHeader("Access-Control-Allow-Origin", "*");
//        response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
//        response.setHeader("Access-Control-Allow-Headers", "Content-Type");
//        response.setHeader("Access-Control-Max-Age", "3600");

int a=insert.insertusera(request);
        System.out.println(a);
        return a;
    }
    @RequestMapping("/chu4")
    public List<buser> chu4() {

        return buserm.selectAll();
    }
    @RequestMapping("/chu5")
    public buser chu5(HttpServletRequest request) {
int id = Integer.parseInt(request.getParameter("id"));
        return  buserm.selectById(id);
    }
    @RequestMapping("/chu6")
    @CrossOrigin(origins = "*", methods = { RequestMethod.GET, RequestMethod.POST })
    public int chu6(HttpServletRequest request) {
int a= Integer.parseInt(request.getParameter("name"));
int b= Integer.parseInt(request.getParameter("totp"));
        return  orderm.insert(a,b);
    }
    @RequestMapping("/chu7")
    @CrossOrigin(origins = "*", methods = { RequestMethod.GET, RequestMethod.POST })
    public List<orders> chu7() {

        return  orderm.selectall();
    }
    @RequestMapping("/chu8")
    @CrossOrigin(origins = "*", methods = { RequestMethod.GET, RequestMethod.POST })
    public void chu8(HttpServletRequest request, HttpServletResponse response) {
        String a = request.getParameter("name");
        String b = request.getParameter("password");
        if (buserm.selectByName(a, b) != null) {

            RequestDispatcher dispatcher = request.getRequestDispatcher("/ouserinf.html");
            try {
                dispatcher.forward(request, response);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            RequestDispatcher dispatcher = request.getRequestDispatcher("/queryall.html");
            try {
                dispatcher.forward(request, response);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    @RequestMapping("/chu9")
    @CrossOrigin(origins = "*", methods = { RequestMethod.GET, RequestMethod.POST })
    public List<buser> chu9() {
        return  buserm.selectwithOrder();
    }
    @RequestMapping("/chu10")
    @CrossOrigin(origins = "*", methods = { RequestMethod.GET, RequestMethod.POST })
    public List<orders> chu10() {

        return  orderm.selectbyid(2);
    }
    @Autowired
    private updata aq;

    @RequestMapping ("/updata")
    public String updateData() {
        try {
            aq.updateUserAndOrder();
            return "Data updated successfully!";
        } catch (Exception e) {
            return "Transaction rolled back: " + e.getMessage();
        }
    }
}
